package com.`as`.common


import android.content.Context
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable


/**
 * Return the application's icon.
 * 获取 App 图标
 * @param packageName The name of the package.
 * @return the application's icon
 */
fun Context.getAppIcon(packageName: String?): Drawable? {
    return  try {
        val pm: PackageManager = packageManager
        val pi = pm.getPackageInfo(packageName!!, 0)
        pi?.applicationInfo?.loadIcon(pm)
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
        null
    }
}

/**
 * 获取包名称
 *
 * @return the application's package name
 */
fun Context.getAppPackageName(): String {
    return packageName
}

/**
 * 获取 App 版本号
 *
 * @param packageName The name of the package.
 * @return the application's version name
 */
fun Context.getAppVersionName(packageName: String?): String {
    return try {
        val pm: PackageManager = packageManager
        val pi = pm.getPackageInfo(packageName!!, 0)
        if (pi == null) "" else pi.versionName
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
        ""
    }
}
